import MediumRangeAntimissileMissile from "../../flyer/DefenseFlyer/MediumRangeAntimissileMissile";
import DestructibleFlyer from "../../flyer/DestructibleFlyer";
import Flyer from "../../flyer/Flyer";
import HealthPoint, { Killer } from "../../funcObj/interface/HealthPoint";
import { Global } from "../../info/Global";
import NodePoolManager from "../../manager/NodePoolManager";
import Player from "../../player/Player";
import DefensiveFacility from "../DefensiveFacility";

const { ccclass, property } = cc._decorator;

/**
 * 蜂刺反导导弹
 * 使用系统的碰撞检测
 * 遇到敌方飞行物或设施时加入目标列表,离开时退出列表,先入先出
 */
@ccclass
export default class MediumRangeAntimissileMissileBase extends DefensiveFacility {

    @property(cc.Node)
    rightPoint: cc.Node = null;

    @property(cc.Node)
    downPoint: cc.Node = null;

    @property(cc.Node)
    readyPoint: cc.Node = null;

    @property(cc.ProgressBar)
    creatProgress: cc.ProgressBar = null;
    /**
     * 入侵者
     */
    intruders: Flyer[];

    creating: boolean;

    missileQueue: Array<cc.Node> = [];

    init(player: Player): void {
        super.init(player);
        this.intruders = [];
        this.creating = false;
        this.creat();
    }

    /**
     * 只攻击飞弹
     * @param other 
     * @param self 
     * @returns 
     */
    onCollisionEnter(other: cc.Collider, self: cc.Collider) {
        //0 设施或飞行物本体
        //1 是攻击范围检测
        if (other.tag == 1 || self.tag == 0) return;

        //只攻击飞行物
        let flyer = other.node.parent.getComponent(DestructibleFlyer);
        if (flyer) {
            if (flyer.player == this.player) return;
            this.intrudersFlyer.push(flyer);
        }
    }

    onCollisionExit(other: cc.Collider, self: cc.Collider) {
        if (other.tag == 1 || self.tag == 0) return;
        let flyer = other.node.parent.getComponent(Flyer);
        if (flyer) {
            let index = this.intrudersFlyer.findIndex(item => item == flyer);
            if (index >= 0) {
                this.intrudersFlyer.splice(index, 1);
            }
        }
    }

    creat() {
        if (this.creating) return;
        this.creatProgress.progress = 0;
        this.creatProgress.node.active = true;
        this.creating = true;
        cc.tween(this.creatProgress)
            .to(this.facilityInfo.制造所需时间, { progress: 1 })
            .call(() => {
                for (let i = 0; i < this.downPoint.childrenCount; i++) {
                    let creatMissile = NodePoolManager.instance.obtain(NodePoolManager.instance.mediumRangeAntimissileMissileBullet);
                    creatMissile.getComponent(MediumRangeAntimissileMissile).init(this);
                    this.missileQueue.push(creatMissile);
                    let flyerParent = this.player.flyerParentNode;
                    creatMissile.parent = flyerParent;
                    creatMissile.angle = this.downPoint.children[i].angle;
                    creatMissile.position = flyerParent.convertToNodeSpaceAR(this.downPoint.children[i].convertToWorldSpaceAR(cc.Vec3.ZERO));
                }

                for (let i = 0; i < this.rightPoint.childrenCount; i++) {
                    let creatMissile = NodePoolManager.instance.obtain(NodePoolManager.instance.mediumRangeAntimissileMissileBullet);
                    creatMissile.getComponent(MediumRangeAntimissileMissile).init(this);
                    this.missileQueue.push(creatMissile);

                    let flyerParent = this.player.flyerParentNode;
                    creatMissile.parent = flyerParent;
                    creatMissile.angle = this.rightPoint.children[i].angle;
                    creatMissile.position = flyerParent.convertToNodeSpaceAR(this.rightPoint.children[i].convertToWorldSpaceAR(cc.Vec3.ZERO));
                }
                this.creatProgress.node.active = false;
            })
            .delay(0.5)
            .call(() => {
                this.creating = false;
            })
            .start();
    }

    protected update(dt: number): void {
        if (this.creating) return;
        let target = this.getClosestTarget();
        if (target)
            this.shot(target);
    }

    /**
     * 射击
     */
    shot(target: HealthPoint) {
        let flyerParent = this.player.flyerParentNode;
        for (let i = 0; i < this.missileQueue.length; i++) {
            let bullet = this.missileQueue[i];
            bullet.position = flyerParent.convertToNodeSpaceAR(bullet.convertToWorldSpaceAR(cc.Vec3.ZERO));
            bullet.parent = flyerParent;
            bullet.getComponent(MediumRangeAntimissileMissile).dispatch(target);
        }
        this.missileQueue = [];
        this.creat();
    }

    die(killer: Killer): void {
        cc.Tween.stopAllByTarget(this.creatProgress);
        this.missileQueue.forEach(item => NodePoolManager.instance.recycle(item))
        super.die(killer);
    }
}
